const moment = require('moment')
module.exports = class DefaultMod extends require('./model') {
  // 获取所有文章列表
  static getAllArticleListMod() {
    let sql =
      'SELECT article.id as id, ' +
      'article.title as title, ' +
      'article.introduce as introduce, ' +
      'article.add_time as addTime, ' +
      'article.view_count as viewCount, ' +
      'article.url as url ,' +
      'tag.tag_Name as tagName ' +
      'FROM article LEFT JOIN tag ON article.tag_id = tag.id'
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 根据文章Id获取文章信息
  static getArticleByIdMod(id) {
    let sql =
      'SELECT article.id as id, ' +
      'article.title as title, ' +
      'article.introduce as introduce, ' +
      'article.content as content, ' +
      'article.add_time as addTime, ' +
      'article.url as url ,' +
      'article.view_count as viewCount, ' +
      'tag.tag_name as tagName, ' +
      'tag.id as tagId, ' +
      'tag.icon as tagIcon ' +
      'FROM article LEFT JOIN tag ON article.tag_id = tag.id ' +
      'WHERE article.id = ' +
      id
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 获取菜单标题名称和编号
  static getTypeInfoMod() {
    let sql = 'SELECT * FROM type'
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 增加网站访客数
  static async addTodayVisitorMod() {
    let today = moment(new Date()).format('YYYY MM DD')
    today = new Date(today).getTime() / 1000
    // 生成近三十天的访问记录
    // for (let i = 29; i >= 0; i--) {
    //   const day = today - 86400 * i
    //   let sql = `INSERT INTO web_visit (visit_date, amount) VALUES (${day}, ${~~(i * Math.random() * 100)})`
    //   await this.query(sql)
    // }
    let sql = `select count(*) as isExist from web_visit where visit_date = ${today}`
    const isExist = await this.query(sql)
    if (isExist[0].isExist) {
      sql = `UPDATE web_visit SET amount = amount + 1 WHERE visit_date = ${today}`
    } else {
      sql = `INSERT INTO web_visit (visit_date, amount) VALUES (${today}, 1)`
    }
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 增加文章访问数
  static async addArticleVisitorMod(articleId) {
    let sql = `UPDATE article SET view_count = view_count + 1 WHERE id = ${articleId}`
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 根据文章类型获取文章
  static getArticleByTagMod(tagId, pageNum, pageSize) {
    pageSize = pageSize || 10
    tagId = tagId || 0
    let sql =
      'SELECT article.id as id, ' +
      'article.title as title, ' +
      'article.introduce as introduce, ' +
      'article.content as content, ' +
      'article.url as url ,' +
      'article.add_time as addTime, ' +
      'article.view_count as viewCount, ' +
      'tag.tag_name as tagName, ' +
      'tag.id as tagId, ' +
      'tag.icon as tagIcon ' +
      'FROM article LEFT JOIN tag ON article.tag_id = tag.id '
    if (tagId != 0) {
      sql += 'WHERE article.tag_id = ' + tagId
    }

    if (pageNum) {
      const start = (pageNum - 1) * pageSize
      sql += ` limit ${start}, ${pageSize}`
    }
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 获取推荐文章
  static getRecommendArticleMod(keywords, num = 5) {
    let sql =
      'SELECT article.id as id, ' +
      'article.title as title, ' +
      'article.introduce as introduce, ' +
      'article.content as content, ' +
      'article.url as url ,' +
      'article.add_time as addTime, ' +
      'article.view_count as viewCount, ' +
      'tag.tag_name as tagName, ' +
      'tag.id as tagId, ' +
      'tag.icon as tagIcon ' +
      'FROM article LEFT JOIN tag ON article.tag_id = tag.id ' +
      `order by ${keywords} desc limit 0, ${num}`
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 获得文章类型及数量
  static getArticleTypeAndAmountMod() {
    let sql = `SELECT tag_id, tag_name, icon, count(*) AS amount FROM article LEFT JOIN tag ON article.tag_id = tag.id GROUP BY tag_id ORDER BY tag_id`
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 添加评论
  static addArticleCommentMod(articleId, author, content, avatar) {
    const add_time = (new Date().getTime() / 1000).toFixed(0)
    let sql = 'INSERT INTO comment (article_id, author, content, avatar, add_time) VALUES (?,?,?,?,?)'
    return new Promise((resolve, reject) => {
      this.query(sql, [articleId, author, content, avatar, add_time])
        .then((res) => resolve(res.insertId))
        .catch((err) => reject(err))
    })
  }

  // 根据文章Id获取文章评论
  static getArticleCommentByIdMod(articleId) {
    let sql = `SELECT * FROM comment WHERE article_id = ${articleId}  ORDER BY add_time desc`
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }

  // 更新评论点赞数
  static updateCommentPreferMod(id, keywords) {
    keywords += 'd'
    const sql = `UPDATE comment SET ${keywords} = ${keywords} + 1 WHERE id = ${id}`
    return new Promise((resolve, reject) => {
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err))
    })
  }
}
